var exec = require('child_process').exec;

/**
 * upload function
 * 
 * @param {any} response 
 * @returns 
 */
function upload(response, postData){
    console.log("request handle upload was called");
    response.writeHead(200, {'Content-Type':'text/plain'});
    response.write("You've sent: " + postData);
    response.end();

    return 'hello upload';
}

function start(response, postData) {
    console.log("request handle start was called");    

    // 模拟阻塞程序
    // function sleep(milliSeconds){
    //     var startTime = new Date().getTime();
    //     while (new Date().getTime() < startTime + milliSeconds);
    // }

    // sleep(5000);

    // exec('ls -al', function(error, stdout, stderr){
    //     var content = stdout;
    //     response.writeHead(200, {"Contebt-Type":"text/plain"});
    //     response.write(content);
    //     response.end();
    // });

    var body = '<html>'+
        '<head>'+ '<meta http-equiv="Content-Type" content="text/html; '+ 'charset=UTF-8" />'+ '</head>'+ '<body>'+ '<form action="/upload" method="post">'+ '<textarea name="text" rows="20" cols="60"></textarea>'+'<input type="file" name="upload" />'+ '<input type="submit" value="Submit text" />'+ '</form>'+ '</body>'+ '</html>';

    response.writeHead(200, {"Content-Type": "text/html"}); 
    response.write(body); 
    response.end();

}

exports.upload = upload;
exports.start = start;